<?php

namespace App\Http\Controllers\Teacher;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Input,Auth;
use App\Model\Subjects;
use App\Model\Grades;
use App\Model\Questions;
use App\Model\Answers;
use App\Http\Requests\QuestionRequest; //引入验证文件/app/Http/Requests/QuestionRequest.php
use App\Tools\UploadFiles; // 上传工具

class QuestionsController extends CommonController
{
    public function index(Request $request,Questions $question){
        //$users = new Users;
        $data['questionCount'] = $question->where('author',Auth::id())->count();//获取总用户数目
        $data['questionAll'] = $question->where('author',Auth::id())->orderBy('id','desc')->with('success')->paginate(10);//所有用户列表
        return view('teacher.questions',$data);
    }

    public function add(QuestionRequest $request,Subjects $subjects,Grades $grades){

        $data['subjects'] = $subjects->all();
        $data['grades'] = $grades->all();

    	// 判断是否post
    	if($request->isMethod('post')){
            
            $question = new Questions;
            $answer = new Answers;

            // 判断选择题是否是图片选择 图片选择题也是选择题
            if($request->is_type == 3){
                $questionData['is_type'] = 1;
            }else{
                $questionData['is_type'] = $request->is_type;
            }

            $questionData['title'] =  $request->title;
            $questionData['author'] =  Auth::id();
            $questionData['subject'] =  $request->subject;
            $questionData['grade'] =  $request->grade;
            $questionData['add_time'] =  time();

            $questionId = $question->insertGetId($questionData); // 获取问题id
            
            // 判断是否是填空
            if($request->is_type == 2){
                $answerData['qid'] = $questionId;
                $answerData['answer'] = $request->answers;
                $answerData['content'] = empty($request->content)?'':$request->content;
                $answerData['is_success'] = 1;
            }

            // 判断选择题
            if($request->is_type == 1){
                foreach($request->answer as $k=>$v){
                    $answerRs['answer'] = $v;
                    $answerRs['content'] = empty($request->content[$k])?'':$request->content[$k];
                    $answerRs['is_success'] = $request->is_success[$k]??0;
                    $answerRs['qid'] = $questionId; // 题目id
                    $answerData[] = $answerRs;
                }
            }

            // 带图片选择题
            if($request->is_type == 3){
                $uploads = new UploadFiles;//实例化上传类
                $uploadData = $uploads->uploads('answer',0,0,0,0);
                foreach($request->answer as $k=>$v){
                    $imagePath = $uploads->uploads('answer',0,0,0,$k);
                    $answerRs['answer_pic'] = $imagePath['bigPath'];
                    $answerRs['content'] = empty($request->content[$k])?'':$request->content[$k];
                    $answerRs['is_pic'] = 1;
                    $answerRs['is_success'] = $request->is_success[$k]??0;
                    $answerRs['qid'] = $questionId; // 题目id
                    $answerData[] = $answerRs;
                }
                
            }
            
            $answer->insert($answerData);// 开始插入答案数据

            return view('teacher.questions_add',$data)->with('msg','添加成功!');
    	}

    	return view('teacher.questions_add',$data);
    }

    // 编辑题目
    public function edit(QuestionRequest $request,Subjects $subjects,Grades $grades,$id){

        $question = new Questions;
        $answer = new Answers;

        $data['subjects'] = $subjects->all();
        $data['grades'] = $grades->all();

        $data['info'] = $question->with('answers')->find($id);

        // 判断是否post
        if($request->isMethod('post')){

            $questionId = $id;

            if($data['info']['is_type'] != $request->is_type){

                $answer->where('qid',$id)->delete();

                // 判断是否是填空
                if($request->is_type == 2){
                    $answerData['qid'] = $questionId;
                    $answerData['answer'] = $request->answers;
                    $answerData['content'] = empty($request->content)?'':$request->content;
                    $answerData['is_success'] = 1;
                }

                // 判断选择题
                if($request->is_type == 1){
                    foreach($request->answer as $k=>$v){
                        $answerRs['answer'] = $v;
                        $answerRs['content'] = empty($request->content[$k])?'':$request->content[$k];
                        $answerRs['is_success'] = $request->is_success[$k]??0;
                        $answerRs['qid'] = $questionId; // 题目id
                        $answerData[] = $answerRs;
                    }
                }

                // 带图片选择题
                if($request->is_type == 3){
                    $uploads = new UploadFiles;//实例化上传类
                    $uploadData = $uploads->uploads('answer',0,0,0,0);
                    foreach($request->answer as $k=>$v){
                        $imagePath = $uploads->uploads('answer',0,0,0,$k);
                        $answerRs['answer_pic'] = $imagePath['bigPath'];
                        $answerRs['content'] = empty($request->content[$k])?'':$request->content[$k];
                        $answerRs['is_pic'] = 1;
                        $answerRs['is_success'] = $request->is_success[$k]??0;
                        $answerRs['qid'] = $questionId; // 题目id
                        $answerData[] = $answerRs;
                    }
                    
                }

                $answer->insert($answerData);// 开始插入答案数据

            }else{

                // 判断是否是填空
                if($request->is_type == 2){
                    $answer->where(['id',$data['info']['answers'][0]['id']])->update(['answer'=>$request->answers]);
                }

                // 判断选择题
                if($request->is_type == 1){
                    foreach($request->answer as $k=>$v){
                        $answerRs['answer'] = $v;
                        $answerRs['content'] = empty($request->content[$k])?'':$request->content[$k];
                        $answerRs['is_success'] = $request->is_success[$k]??0;
                        $answer->where('id',$data['info']['answers'][$k]['id'])->update($answerRs);
                    }
                }

                // 带图片选择题
                if($request->is_type == 3){
                    $uploads = new UploadFiles;//实例化上传类
                    $uploadData = $uploads->uploads('answer',0,0,0,0);
                    foreach($request->answer as $k=>$v){
                        $imagePath = $uploads->uploads('answer',0,0,0,$k);
                        $answerRs['answer_pic'] = $imagePath['bigPath'];
                        $answerRs['content'] = empty($request->content[$k])?'':$request->content[$k];
                        $answerRs['is_pic'] = 1;
                        $answerRs['is_success'] = $request->is_success[$k]??0;
                        $answer->where('id',$data['info']['answers'][$k]['id'])->update($answerRs);
                    }
                    
                }

            }

            $questionArr['title'] = $request->title;
            $questionArr['grade'] = $request->grade;
            $questionArr['subject'] = $request->subject;
            $questionArr['is_type'] = $request->is_type;

            $question->where('id',$id)->update($questionArr);

            return redirect('/Teacher/questions/list');
            
        }

        return view("teacher.questions_edit",$data);
    }


    public function del($id){
        $question = new Questions;
        $answer = new Answers;

        $question->where('id',$id)->delete();
        $answer->where('qid',$id)->delete();
        return redirect('/Teacher/questions/list');
    }

}
